package com.isoftstone.interceptor;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import com.isoftstone.service.system.escbocop.EscbocopService;


/**
 * 类名称：XYHandlerInterceptor.java
 * 类描述： 网页访问拦截器
 *
 * @author cc
 * 创建时间：2016年1月14日10:29:53
 */
public class XYHandlerInterceptor extends HandlerInterceptorAdapter {



    Logger logger = LoggerFactory.getLogger(XYHandlerInterceptor.class);



    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取登录成功后的accessToken11
        logger.info("微信端拦截器");


        if (request.getMethod().equals("OPTIONS")) {
            return true;
        }
        /**
         * 登录中银开发平台的，获取回传的 accessToken，userid
         * 后续调用接口使用
         * 
         */


        String path = request.getRequestURI();
        logger.info("path = " + path);
        //登录后重定向地址

        String url = null;

        if ("1".equals(BaseController.userContextPathFlag)){
            url =   BaseController.basePath + request.getRequestURI();
        } else {
            url =   request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getRequestURI();
        }

       // String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getRequestURI();
        if (request.getQueryString() != null) {
            url = url + "?" + request.getQueryString();
        }


        System.out.println("sessionid ======" + request.getSession().getId());


        Cookie[] cookies = request.getCookies();

        for (Cookie cookie : cookies) {
            System.out.println("cookieName = " + cookie.getName());
            System.out.println("cookieValue = " + cookie.getValue());
        }


        String accessToken = request.getParameter("accessToken");
        String userid = request.getParameter("userid");

        logger.info("==========accessToken==========="+accessToken);


        if (path.toLowerCase().contains("app/index")) {


            /**
             * bugfix 不需要if判断，要不然切换用户的时候有问题
             */

            //if(request.getSession().getAttribute("accessToken")==null){

                request.getSession().setAttribute("accessToken",accessToken);
                request.getSession().setAttribute("userid",userid);
                request.getSession().setAttribute("mobileno",userid);

                
            //}
            
        }


        if (path.toLowerCase().contains("app/admin")) {


            logger.info("==========Authorization==========="+request.getHeader("acton"));
            logger.info("==========userid==========="+request.getHeader("userid"));

            String clienttoken = request.getHeader("acton");

            String serveroken = (String) request.getSession().getAttribute("accessToken");


            /**
             * 效验服务器端和客户端token是否一致
             */

            System.out.println("serveroken = " + serveroken);
            System.out.println("clienttoken = " + clienttoken);

            if(!StringUtils.equals(clienttoken,serveroken))  {


                 logger.info("========前后端token不一致============");
                 response.setStatus(401);
                 return false;

             } else {
                 return true;
             }

        } else {
            return true;
        }
    }


}
